In order to decomopose a time series, the first step in most algorithms is to produce an estimate of the trend. This is because producing an estimate of the trend given only the time is in most cases easier than producing an estimate of any of the other components.
In this notebook we are goin to study how to produce estimates of the trend using the moving average, which is what the classical decomposition algorithm does.
Moving averages are a concept that extends beyond time series decomposition and hence additional points will also be discussed.
The concept of a moving average
Given a time series \(y_t\), a moving average of \(y_t\) is another time series that is built by averaging the time series at each point in time using a particular window definition. At each point in time, the values of the time series at a set of neighboring points contained in a specific window of our choice will be averaged.
First example
Consider the example below. We have:
a time series \(y_t\) in
centered windows of 7 points to compute the moving average.
To compute the moving average at an instant \(t\) (instant highlighted in green) we average the values of the time series at \(t-3, t-2, t-1, t, t+1, t+2\) and \(t+3\).
In this specific instance we are using a centered window, in which we leave an equal number of points to the left and to the right of the point at which we are computing the moving average. We will see that there are other alternatives.
By shifting this window we could compute the value of the moving average time series at every instant in time. For example, to compute the value of the moving average at point \(t+1\) instead of \(t\), we need to shift the window used for point \(t\) one timestep to the right. This results in the purple window in the figure below. The corresponding formula for the 7-MA at \(t+1\) has also been included.
Important difference between windows at \(t\) and at \(t+1\) (neighboring windows)
These windows differ in two points.
The window for \(t\) includes \(y_{t-3}\) but not \(y_{t+4}\).
The window for \(t+1\) includes \(y_{t+4}\) but not \(y_{t-3}\).
In general, two neighboring windows will differ in only two points. This will be important to understant the effect of window sizes later in this notebook.
Limitation of centered windows
The points at the extremes do not have complete windows and therefore we will not compute the moving average at these points.
Centered moving averages
Windows of uneven number of points
Centered moving averages are of particular interest to us because centered windows tend to minimize the errors of approximations in general. Also, classical decomposition uses centered moving averages to estimate the trend.
Centered moving average of order m can be written as:
\(m=2k+1\) is the number of points in the window. Because we are using centered windows, we require the number of points \(m\) to be uneven (hence \(2k+1\)). In this manner we will be able to leave as many points to the left as to the right.
the sum runs from \(j=-k\) to \(j=+k\) and passes through \(k=0\) (hence \(2k+1\) points).
For example, a 7 order moving average like the one in the example before would have the following formula:
In R, we are going to compute moving averages using the function slider::slide_dbl(). We will use slider::slide_dbl() within mutate to define new columns. This function takes the following arguments:
The first argument is the name of the column containing the time series for which we wish to compute the moving average.
The second argument is the aggregate function we wish to apply to the points covered by the window. Since we are computing moving averages, we will pass the function mean.
The argument .before specifies the number of points in the window to the left of the point at which we are computing the moving average. In the example above (a \(7-MA_t\)), this number is 3.
The argument .after specifies the number of points in the window to the right of the point at which we are computing the moving average. In the example above (a \(7-MA_t\)), this number is 3.
Look at the code below and make sure you understand how the \(7-MA_t\) is computed in the example below:
aus_exports <- global_economy %>%filter(Country =="Australia") %>%# Generate the moving averagemutate(#The slider function applies a function to "sliding" time windows.#In this case it is a 7-MA because we are moving from j=-3 (before = 3) # to j=+3 (after = 3).`7-MA`= slider::slide_dbl(Exports, mean,#.complete = TRUE -> function evaluated only on full windows# This means that the MA will not be computed for the first# and last three points of the time series.before =3, .after =3, .complete =TRUE) ) %>%select(Country, Code, Year, Exports, `7-MA`)aus_exports
# A tsibble: 58 x 5 [1Y]
# Key: Country [1]
Country Code Year Exports `7-MA`
<fct> <fct> <dbl> <dbl> <dbl>
1 Australia AUS 1960 13.0 NA
2 Australia AUS 1961 12.4 NA
3 Australia AUS 1962 13.9 NA
4 Australia AUS 1963 13.0 13.3
5 Australia AUS 1964 14.9 13.3
6 Australia AUS 1965 13.2 13.3
7 Australia AUS 1966 12.9 13.0
8 Australia AUS 1967 12.9 13.0
9 Australia AUS 1968 12.3 12.7
10 Australia AUS 1969 12.0 12.6
# ℹ 48 more rows
Let us conduct a sanity check. We are going to extract the first seven elements of Exports, average them and check that they are actually equal to the first element of the 7-MA series
Let us now proceed to create different centered moving averages for this time series. For each moving average, we will use a different number of points. Note that we are using always odd numbers to ensure that there is the same amount of points to the left than to the right:
aus_exports <- global_economy %>%filter(Country=='Australia')# Create the different Moving Averages of order i.# Note that i is always unevenfor (i inseq(3, 13, by =2)){ col_name <-paste0(as.character(i), "-MA") width = (i-1)/2# Number of points to be left to the left an to the right aus_exports[[col_name]] = slider::slide_dbl(aus_exports$Exports, mean,#.complete = TRUE -> function evaluated only on full windows.before = width, .after = width, .complete =TRUE)}aus_exports <- aus_exports %>%select(Exports, `3-MA`, `5-MA`, `7-MA`, `9-MA`, `11-MA`, `13-MA`)aus_exports
# A tsibble: 58 x 8 [1Y]
Exports `3-MA` `5-MA` `7-MA` `9-MA` `11-MA` `13-MA` Year
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 13.0 NA NA NA NA NA NA 1960
2 12.4 13.1 NA NA NA NA NA 1961
3 13.9 13.1 13.5 NA NA NA NA 1962
4 13.0 14.0 13.5 13.3 NA NA NA 1963
5 14.9 13.7 13.6 13.3 13.2 NA NA 1964
6 13.2 13.7 13.4 13.3 13.1 13.1 NA 1965
7 12.9 13.0 13.3 13.0 13.1 13.0 13.0 1966
8 12.9 12.7 12.7 13.0 13.0 13.1 13.1 1967
9 12.3 12.4 12.6 12.7 13.0 13.1 13.1 1968
10 12.0 12.4 12.6 12.6 12.9 13.1 13.2 1969
# ℹ 48 more rows
If you depict the different moving averages we produced, you would get the graphs below.
Note that:
A larger number of points in the moving average result in a smoother curve.
This is because more points are included in the averaging window. With each movement of the window, only two of the points in the window change. Therefore the more points contained in the window, the slower the computation of the moving average changes with each window shift.
Because we are using centered moving averages and we have required our windows to be complete (we used .complete = TRUE), no values are computed for the first \((m-1)/2\) points or the last \((m-1)/2\) points.
Windows with an even number of points
In our previous discussion we have required that \(m = 2k+1\). In other words, that m be uneven. This condition ensured that we had the same amount of data points to the left and to the right of the point whose moving average we wish to compute. This is great when using centered moving average.
How can we compute the centered moving average of data if we wish to base the average on an even number of datapoints?
This problem is important becasue, for series with seasionality, we want to average over the whole seasonal period.
for weekly data (period length of 7) we can compute a centered moving average with the same amount of points to both sides.
for monthly data (period length of 12) or quarterly data (period length of 4) a centered moving average cannot be directly computed. We cannot leave the same amount of points to the left and to the right.
If the number of points is even, we cannot use a centered window. That is, if \(m\) (the number of points contained in the moving average window) is even, then we will have either one more point to the left or one more point to the right of the point at which we are computing the moving average.
There is a way around this, which is to:
Compute a moving average using these unbalanced windows.
Compute the 2-MA of the result of 1. That is, compute the 2nd order moving average of the previously computed moving average. We will see that this will result in a weighted moving average, which is the best we will be able to do in these instances.
Let us look at an example in the figure below which shall clarify the process. We would like to compute a 4-MA (Window of 4 points) and make it centered following the process outlined above:
We start by computing the \(4-MA_t\) with a window that picks 1 point to the left of the point at which we are computing the MA and two points to the right. This would be the green window in the figure above. Using this window we compute the \(4-MA\) at every point, wherever we can build a complete window.
Output: a time series \(4-MA_t\). At each point in time the \(4-MA_t\) is computed as:
We compute a \(2-MA\) of the foregoing \(4-MA_t\). To compensate the fact that initially we had one more point to the right than to the left, we average the \(4-MA_t\) and one point to its left (the \(4-MA_{t-1}\)).
Output: a time series \(2x4-MA_t\). At each point in time, this is computed as:
We may express this equation in the following form:
\[
\sum_{j=-k}^{j=k}{a_jy_{t+j}}
\]
where:
\(a_j\) are the coefficients associated to each \(y_j\). In this case, the list of \(a_j\) would be \([\frac{1}{8},\frac{1}{4},\frac{1}{4},\frac{1}{4},\frac{1}{8}]\).
\(k = m/2\) (in this case \(m\) is even, so it can be divided by 2)
We can see that these coefficients \(a_j\) satisfy some important properties:
\(\sum_{j=-k}^{j=k}{a_j} = 1\). The weights add up to 1
\(a_j= a_{-j}\). The weights are symmetric.
Because of these two properties we may say that this sum constitutes a weighted average, in fact a symmetric weighted average.
Finally, the procedure for even number of points we have shown here generalizes to any window with an even number of points, that is:
A 2 x m-MA (\(m\) even) is equivalent a centered weighted moving average of \(m+1\) points, where all observations take weights \(1/m\), except for the first and last term, which take weights \(1/(2m)\).
The resulting coefficients for some relevant examples are:
For quarterly data (m=4): \(\big[\frac{1}{8}, \frac{1}{4}, \frac{1}{4}, \frac{1}{4}, \frac{1}{8}\big]\)
For monthly data (m=12): \(\big[\frac{1}{24}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{12}, \frac{1}{24}\big]\)
…
One more comment about this topic: in the 2 x 4-MA example we showed, there was an arbitrary choice: the initial window for the \(4-MA_t\) used 1 point to the left and two points to the right. We could have started with a window that used 2 points to the left and 1 point to the right though. We would have had to compensate this picking one point to the right in the subsequent \(2-MA\). The image below clarifies this:
Example in R
Let us apply this procedure to the quarterly production of beer in australia beyond 1992:
# Filter datasetbeer <- aus_production %>%filter(year(Quarter) >=1992) %>%select(Quarter, Beer)beer <- beer %>%mutate(# Unbalanced window for the first 4-MA: 1 point to the left, two# points to the right`4-MA`= slider::slide_dbl(Beer, mean,.before =1, .after =2, .complete =TRUE),# Subsequent two MA to make the overall window balanced`2x4-MA`= slider::slide_dbl(`4-MA`, mean,.before =1, .after =0, .complete =TRUE) )beer %>%select(Quarter, Beer, `4-MA`, `2x4-MA`) %>%head(5)
# A tsibble: 5 x 4 [1Q]
Quarter Beer `4-MA` `2x4-MA`
<qtr> <dbl> <dbl> <dbl>
1 1992 Q1 443 NA NA
2 1992 Q2 410 451. NA
3 1992 Q3 420 449. 450
4 1992 Q4 532 452. 450.
5 1993 Q1 433 449 450.
# A tsibble: 5 x 4 [1Q]
Quarter Beer `4-MA` `2x4-MA`
<qtr> <dbl> <dbl> <dbl>
1 2009 Q2 398 430 430
2 2009 Q3 419 430. 430.
3 2009 Q4 488 424. 427.
4 2010 Q1 414 NA NA
5 2010 Q2 374 NA NA
Now let us depict the moving average along with the original series:
beer %>%autoplot(Beer) +geom_line(aes(y =`2x4-MA`), colour ="#D55E00") +labs(y ="Production of beer (megalitres)",title ="Production of beer in Australia starting on 1992")
We use the centered moving average because, in general, using centered approximations results in smaller errors when computing numerical approximations. However, when approaching the extremes of our data it will not be possible to use centered windows any more and we would have to gradually shift to the left or right moving average.
Because of this, algorithms using window functions usually offer the option to transition to a one-sided (left or right) moving average as they approach the extremes (beggining and end) of the time series:
Left moving average: window considers the point in time at which we are computing and points to its left
\[
\frac{1}{m}\sum_{k=-(m-1)}^{j=0}{y_{t+j}}
\]
Right moving average: window considers the point in time at which we are computing and points to its right:
\[
\frac{1}{m}\sum_{k=0}^{j=m-1}{y_{t+j}}
\]
In the context of this course, we will only use centered moving averages. But you need to know that there are alternatives to it.
Exercise 1
The time series below measures the number of employed people in retail in the US beyond 1990:
# Filter the series and select relevant columnsus_retail_employment <- us_employment %>%filter(year(Month) >=1990, Title =="Retail Trade") %>%select(-Series_ID)
Compute the 2x12 Moving Average of the time series Employed contained in us_retail_employment.
Take the dataset vic_elec and aggregate the data to obtain the mean quarterly half hourly demand using index_by(). That is, for each quarter, compute the mean half-hourly demand.
Then compute a 2x4-MA of the resulting series that is centered.
# A tsibble: 12 x 4 [1Q]
quarter q_demand `4-MA` `2x4-MA`
<qtr> <dbl> <dbl> <dbl>
1 2012 Q1 4776. NA NA
2 2012 Q2 4843. NA NA
3 2012 Q3 4898. 4737. 4737.
4 2012 Q4 4430. 4738. 4723.
5 2013 Q1 4780. 4709. 4688.
6 2013 Q2 4728. 4667. 4659.
7 2013 Q3 4731. 4651. 4638.
8 2013 Q4 4364. 4625. 4606.
9 2014 Q1 4676. 4587. 4600.
10 2014 Q2 4577. 4613. 4612.
11 2014 Q3 4835. 4610. NA
12 2014 Q4 4352. NA NA
# Check that you can produce the same result inverting the order of before-after# The output of the final time series 2x12 MA is the sameq_elec <- q_elec %>%mutate(`4-MA_alt`= slider::slide_dbl(q_demand, mean,.before =1, .after =2, .complete =TRUE),`2x4-MA_alt`= slider::slide_dbl(`4-MA_alt`, mean,.before =1, .after =0, .complete =TRUE) )# Check that both vectors match exactlyall.equal(q_elec$`2x4-MA`, q_elec$`2x4-MA_alt`)
[1] TRUE
Exercise 3
Deduce the formula for a \(3x7-MA\) of a time series \(y_t\). That is, first take a 7-MA and then the 3-MA of that 7-MA.
Exercise 4
Deduce the formula for a \(2x8-MA\) of a time series \(y_t\). That is, first take an 8-MA and then compute a 2-MA of that 8-MA. Ensure that the end result corresponds to a centered moving average that has the same number of points to the left and to the right.
Blank picture to practice
I include here this blank picture of a time series so that you can practice with pen and paper drawing the windows required for exercises 3 and 4 and obtaining the formulas by hand.